#include "widget.h"

#include <QApplication>
#include <QDebug>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <QSocketNotifier>
#include <QTimer>
#include <QFile>
enum InputCode {
    UP = 103,
    LEFT = 106,
    RIGHT = 105,
    DOWN = 108,
    MENU = 139,
    TOSAFE = 115,
    WITHDRAW = 250,
    CANCEL = 223,
    CONFIRM = 28,
    LIGHT = 542,
    TOUCH = 530,
    LAUCH = 249,
    MSL_OFF = 116,
    EMG_STOP = 128
};
void handleInputEvents(int fd, Widget *widget) {
    struct input_event ev;
    ssize_t n = read(fd, &ev, sizeof(ev));

    QColor c = QColor(Qt::red);
    if (n == sizeof(ev)) {
        if (ev.value == 0) c.setAlpha(0);
        QTimer::singleShot(100, [ev, widget, c]()
        {
            switch (ev.code) {
            case UP:
                widget->setRectangleParams(CONV(6), CONV(5), CONV(4), CONV(2), c);
                break;
            case LEFT:
                widget->setRectangleParams(CONV(3), CONV(8), CONV(4), CONV(2), c);
                break;
            case RIGHT:
                widget->setRectangleParams(CONV(9), CONV(8), CONV(4), CONV(2), c);
                break;
            case DOWN:
                widget->setRectangleParams(CONV(6), CONV(11), CONV(4), CONV(2), c);
                break;
            case MENU:
                widget->setRectangleParams(CONV(6), CONV(14), CONV(4), CONV(2), c);
                break;
            case TOSAFE:
                widget->setRectangleParams(CONV(6), CONV(17), CONV(4), CONV(2), c);
                break;
            case WITHDRAW:
                widget->setRectangleParams(CONV(6), CONV(20), CONV(4), CONV(2), c);
                break;
            case CANCEL:
                widget->setRectangleParams(CONV(6), CONV(23), CONV(4), CONV(2), c);
                break;
            case CONFIRM:
                widget->setRectangleParams(CONV(50), CONV(8), CONV(4), CONV(2), c);
                break;
            case LIGHT:
                widget->setRectangleParams(CONV(50), CONV(11), CONV(4), CONV(2), c);
                break;
            case TOUCH:
                widget->setRectangleParams(CONV(50), CONV(14), CONV(4), CONV(2), c);
                break;
            case LAUCH:
                widget->setRectangleParams(CONV(50), CONV(17), CONV(4), CONV(2), c);
                break;
            case MSL_OFF:
                widget->setRectangleParams(CONV(50), CONV(20), CONV(4), CONV(2), c);
                break;
            case EMG_STOP:
                widget->setRectangleParams(CONV(50), CONV(23), CONV(4), CONV(2), c);
                break;
            default:
                break;
            }

        });
    }
}
// 处理 GPIO 状态变化的函数
void handleGpioChange(const QString& gpioPath, Widget *widget) {
    QFile file(gpioPath);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QByteArray data = file.readAll();
        file.close();
        int value = data.trimmed().toInt();

        QColor color;
        if (gpioPath.contains("gpio24")) {
            color = QColor(Qt::green);
            if (value != 1) {
                color.setAlpha(0);
            }
            widget->setRectangleParams(CONV(48), CONV(5), CONV(1), CONV(2), color);
        } else if (gpioPath.contains("gpio125")) {
            color = QColor(Qt::red);
            if (value != 1) {
                color.setAlpha(0);
            }
            widget->setRectangleParams(CONV(49), CONV(5), CONV(1), CONV(2), color);
        }
    }
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    // 打开输入设备文件
    int fd = open(argv[1], O_RDONLY | O_NONBLOCK);
    if (fd == -1) {
        qDebug() << "Failed to open /dev/input/eventX";
        return -1;
    }

    // 创建 QSocketNotifier 监听文件描述符的可读事件
    QSocketNotifier *notifier = new QSocketNotifier(fd, QSocketNotifier::Read, &w);
    QObject::connect(notifier, &QSocketNotifier::activated, [fd, &w](int socket) {
        handleInputEvents(fd, &w);
    });

    // 绘制绿灯
    QColor color = QColor(Qt::green);
    w.setRectangleParams(CONV(48), CONV(5), CONV(1), CONV(2), color);
    qDebug() << CONV(48)<< CONV(5)<< CONV(1)<< CONV(2);

    // 连接 aboutToQuit 信号，在程序退出时执行清理操作
    QObject::connect(&a, &QApplication::aboutToQuit, [&]() {
        // 写入 gpio24/value 文件
        QFile gpio24File("/sys/class/gpio/gpio24/value");
        if (gpio24File.open(QIODevice::WriteOnly | QIODevice::Text)) {
            gpio24File.write("1");
            gpio24File.close();
        } else {
            qDebug() << "Failed to open /sys/class/gpio/gpio24/value for writing";
        }

        // 写入 gpio125/value 文件
        QFile gpio125File("/sys/class/gpio/gpio125/value");
        if (gpio125File.open(QIODevice::WriteOnly | QIODevice::Text)) {
            gpio125File.write("0");
            gpio125File.close();
        } else {
            qDebug() << "Failed to open /sys/class/gpio/gpio125/value for writing";
        }

        // 关闭输入设备文件描述符
        close(fd);
    });

    int ret = a.exec();

    return ret;
}
